import axios from 'axios'
import { Message } from 'element-ui'
import store from '@/store'
// create an axios instance
const service = axios.create({
  baseURL: process.env.VUE_APP_BASE_API,
  timeout: 60000 // request timeout
})

service.interceptors.request.use(
  config => {
    const merchant_id = store.getters['zcsd-core/checkProject'].merchant_id
    if (merchant_id) {
      // 目前仅翻译、小智桌面的基础域名才需要加MerchantId
      config.headers['MerchantId'] = merchant_id
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
)
// response interceptor
service.interceptors.response.use(
  response => {
    return Promise.resolve(response?.data)
  },
  error => {
    if (error.data && !error.config.noMsg) {
      const data = error.data
      const msg = error.message || data.msg
      Message({
        message: msg,
        type: 'error'
      })
    }

    return Promise.reject(error)
  }
)

export default service
